﻿using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using XFSenseUI.XFItems.SenseControls;

namespace XFSenseUI.XFItems
{
    public class SenseItemNumber : SenseItemBase
    {
        public SenseControlNumber Control = new SenseControlNumber();
        public int Value { get { return Control.Value; } set { Control.Value = value; } }
        public int MaxValue { get { return Control.MaxValue; } set { Control.MaxValue = value; } }
        public int MinValue { get { return Control.MinValue; } set { Control.MinValue = value; } }

        private int _height = -1;
        private int _oldValue;
        private bool _inControl;

        public SenseItemNumber()
        {
            ItemType = XFItemType.Clickable;
            Style = new XFItemStyle
                    {
                        BorderBottomColor = Color.FromArgb(189, 182, 189),
                        DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                        TextColor = Color.Black,
                        TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                        SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                        SelectedBackgroundImageString = "item-select-background",
                        SecondaryTextColor = Color.FromArgb(57, 52, 57),
                        SelectedTextColor = Color.White,
                        SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                        SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                        Padding = 11,
                        PaddingBottom = 12,
                        PaddingLeft = 10,
                        PaddingRight = 16
                    };

            Control.Owner = this;

            Click += (o, e) => Control.OnClick();
            Focus += (o, e) => Control.OnFocus();
            Blur += (o, e) => Control.OnBlur();
            Refresh += (o, e) => Control.OnRefresh();
            Disposed += (o, e) => Control.Dispose();
        }

        public override XFItemClickResult GetClickResult()
        {
            var modal = new SenseControlNumberBig {Value = Control.Value};
            _oldValue = Control.Value;
            modal.MouseUp += (o, e) =>
                {
                    Invalidate();
                    Control.Value = modal.Value;

                    if (_oldValue != Value)
                        OnChange();
                };

            if (!_inControl)
                Parent.AddEffect(modal);

            _inControl = false;
            return Control != null ? Control.GetClickResult() : base.GetClickResult();
        }

        public override void OnBlur()
        {
            base.OnBlur();
            Control.OnBlur();
        }

        public override void ResetHeight()
        {
            _height = -1;
            if(Parent != null)
                ((XFPanelList)Parent).Resize();
        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            int size = Control.GetHeight() + Style.PaddingTop + Style.PaddingBottom;
            _height = CalcRecs(Control.ItemRectangle.Width + Style.PaddingLeft);

            _height = (_height > size ? _height : size);

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), _height);

            return _height;
        }

        public override void OnMouseDown(int x, int y)
        {
            base.OnMouseDown(x, y);
            if (Control == null) return;
            _oldValue = Value;
            int width = GetWidth();
            Rectangle rec = Control.ItemRectangle;
            if (rec.Width != 0)
                width -= rec.Width + Style.PaddingRight;
            int aHeight = (GetHeight() >> 1) - (rec.Height >> 1);

            rec.X = width;
            rec.Y = aHeight;

            _inControl = rec.Contains(x, y);
            if (_inControl)
                Control.OnMouseDown(x - width, y - aHeight);
        }

        public override void OnMouseMove(int x, int y)
        {
            base.OnMouseMove(x, y);
            if (Control != null)
            {
                int width = GetWidth();
                Rectangle rec;
                if ((rec = Control.ItemRectangle).Width != 0)
                    width -= rec.Width + Style.PaddingRight;
                int aHeight = (GetHeight() / 2) - (rec.Height / 2);

                rec.X = width;
                rec.Y = aHeight;

                if (rec.Contains(x, y))
                    Control.OnMouseMove(x - width, y - aHeight);
            }
        }

        public override void OnMouseUp()
        {
            base.OnMouseUp();
            if (Control != null)
                Control.OnMouseUp();

            if (_oldValue != Value)
                OnChange();
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            var tempState = State;
            if (_inControl && State != XFItemState.Disabled)
                State = XFItemState.Normal;
            base.ItemPaint(g, x, y);

            State = tempState;

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);

            var rec = new Rectangle();
            int width = _mRec.X + _mRec.Width + Style.PaddingLeft;

            if (Control != null) rec = Control.ItemRectangle;

            if (!string.IsNullOrEmpty(MainText))
                g.DrawString(MainText, _mRec, Style, fontMain, colorMain);
            if (!string.IsNullOrEmpty(SecondaryText))
                g.DrawString(SecondaryText, _sRec, Style, fontSecondary, colorSecondary);

            if (rec.Width != 0 && Control != null)
            {
                int aHeight = (GetHeight() >> 1) - (rec.Height >> 1);
                Control.ItemPaint(g, width, aHeight);
            }

            if (!string.IsNullOrEmpty(Icon))
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _iconSize, _iconSize));
        }
    }
}